import router from '@/router'
import store from '@/store'
import { Message } from 'element-ui'
router.beforeEach((to, from, next) => {
  // console.log("to", to)
  const Whitelist = ['/login', '/404']
  const token = store.getters.token
  const MenuList = store.state.user.MenuList
  const tokentime = Number(store.state.user.tokentime)
  const timeOver = 4 * 3600 * 1000
  const date = new Date().getTime()
  // 修改tag值
  to.meta.title = to.query.metatitle || to.meta.title
  if (token) {
    // 如果token过期
    if (date > tokentime + timeOver) {
      Message.warning('登录已过期,请重新登陆')
      store.dispatch('user/logout')
      next(`/login?redirect=${router.history.current.fullPath}`)
      store.commit('tagsView/REM_VISVIW', [])
    } else {
      if (to.path == '/login') {
        next('/')
      } else {
        const list = [
          '/Fourth/ProjectEstablishment/ImportDetails',
          '/Fourth/EmployeeHome/type1',
          '/Fourth/EmployeeHome/type2',
          ,
          '/Fourth/EmployeeHome/type3',
          '/Fourth/Tasklist/TasklistAdd',
          '/Fourth/Tasklist/TasklistAddC',
          '/Fourth/Tasklist/ImportDetailsC'
        ]
        list.forEach(item => {
          MenuList.push({ isa9common: item })
        })
        // console.log("MenuList", MenuList);
        const Blan = MenuList.some(item => {
          return item.isa9common == to.path
        })

        if (!Blan) {
          if (MenuList[0].isa9common != null) {
            next(MenuList[0].isa9common)
          } else {
            next(MenuList[1].isa9common)
          }
        } else {
          next()
        }
      }
    }
  } else {
    if (Whitelist.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
